﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;

namespace AntUnion.FrameWork.Web.Common
{
    public static class IHtmlHelperExtension
    {

        #region Image控件
        /// <summary>
        /// Image控件（不可用）
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Name">图片名称（如：temp.jpg）</param>
        /// <returns>返回：<img>Html代码</returns>
        public static IHtmlContent CusImg(this IHtmlHelper helper, string Name)
        {
            return CusImg(helper, Name, "alt =''");
        }
        /// <summary>
        /// Image控件（不可用）
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Name">图片名称（如：temp.jpg）</param>
        /// <param name="Attributes">附加属性值，比如onclick=""之类</param>
        /// <returns>返回：<img>Html代码</returns>
        public static IHtmlContent CusImg(this IHtmlHelper helper, string Name, string Attributes)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<img");
            sb.Append(" src =\"/Images/" + Name + "\"");
            sb.Append(" " + Attributes.Trim());
            sb.Append(" />");
            return new HtmlString(sb.ToString());
        }
        #endregion Image控件

        #region 自定义分页控件
        /// <summary>
        /// 自定义分页控件
        /// </summary>
        /// <param name="helper">但前页对象</param>
        /// <param name="PageIndex">但前页</param>
        /// <param name="PageSize">每页显示的记录数</param>
        /// <param name="TotalCount">总记录数</param>
        /// <param name="TotalPages">总页数</param>
        /// <param name="urlPrefix">Url</param>
        /// <returns>分页控件</returns>
        public static IHtmlContent CusPager(this IHtmlHelper helper, int PageIndex, int PageSize, int TotalCount, int TotalPages, string urlPrefix, string paramFix)
        {
            if (TotalPages == 0)
                return new HtmlString("");
            //判断总页数为1的话就不显示分页
            if (TotalPages == 1)
                return new HtmlString("");
            StringBuilder sb = new StringBuilder();

            PageIndex = PageIndex < 1 ? 1 : PageIndex;                                                          //但前页如果小于1直接赋值为1
            PageIndex = PageIndex > TotalPages ? TotalPages : PageIndex;                       //但前页如果大于总页数直接赋值为最后一页
            int start = TotalPages - PageIndex >= 2 ? PageIndex - 2 : TotalPages - 4;        //设置要显示出来的第一个页码

            sb.AppendLine("<ul class=\"w3-pagination\">");

            //设置上一页
            if (PageIndex > 1)
            {
                sb.AppendLine(string.Format("<li><a href=\"{0}/?page={1}{2}\">&laquo;</a></li>", urlPrefix, PageIndex - 1, paramFix));
            }


            //设置要显示的页码


            if (TotalPages > 7)
            {
                int end = 1;
                while (end <= 5 && end <= TotalPages)
                {
                    if (start == PageIndex)
                    {
                        sb.AppendLine(string.Format("<li><a class=\"w3-green\" href=\"#\">{0}</a></li>", start));
                        start++;
                        end++;
                    }
                    else if (start > 0)
                    {
                        sb.AppendLine(string.Format("<li><a href=\"{0}/?page={1}{2}\">{1}</a></li>", urlPrefix, start, paramFix));
                        start++;
                        end++;
                    }
                    else
                    {
                        start++;
                    }
                }
            }
            else
            {
                for (int i = 1; i <= TotalPages; i++)
                {
                    if (i == PageIndex)
                    {
                        sb.AppendLine(string.Format("<li><a class=\"w3-green\" href=\"#\">{0}</a></li>", i));
                    }
                    else
                    {
                        sb.AppendLine(string.Format("<li><a href=\"{0}/?page={1}{2}\">{1}</a></li>", urlPrefix, i, paramFix));
                    }
                }
            }


            //设置下一页
            if (PageIndex < TotalPages)
            {
                sb.AppendLine(string.Format("<li><a href=\"{0}/?page={1}{2}\">&raquo;</a></li>", urlPrefix, PageIndex + 1, paramFix));
            }


            return new HtmlString(sb.ToString());
        }

        public static IHtmlContent CusPagerFront(this IHtmlHelper helper, int PageIndex, int PageSize, int TotalCount, int TotalPages, string urlPrefix)
        {
            if (TotalPages == 0)
                return new HtmlString("");
            //判断总页数为1的话就不显示分页
            if (TotalPages == 1)
                return new HtmlString("");
            StringBuilder sb = new StringBuilder();

            PageIndex = PageIndex < 1 ? 1 : PageIndex;                                                          //但前页如果小于1直接赋值为1
            PageIndex = PageIndex > TotalPages ? TotalPages : PageIndex;                       //但前页如果大于总页数直接赋值为最后一页
            int start = TotalPages - PageIndex >= 2 ? PageIndex - 2 : TotalPages - 4;        //设置要显示出来的第一个页码

            sb.AppendLine("<ul class=\"pagination\">");

            //设置上一页
            if (PageIndex > 1)
            {
                sb.AppendLine(string.Format("<li><a href=\"javascript:doSearch('{0}',{1});\" >&laquo;</a></li>", urlPrefix, PageIndex - 1));
            }


            //设置要显示的页码


            if (TotalPages > 7)
            {
                int end = 1;
                while (end <= 5 && end <= TotalPages)
                {
                    if (start == PageIndex)
                    {
                        sb.AppendLine(string.Format("<li class=\"active\"><span>{0}</span></li>", start));
                        start++;
                        end++;
                    }
                    else if (start > 0)
                    {
                        sb.AppendLine(string.Format("<li><a href=\"javascript:doSearch('{0}',{1});\">{1}</a></li>", urlPrefix, start));
                        start++;
                        end++;
                    }
                    else
                    {
                        start++;
                    }
                }
            }
            else
            {
                for (int i = 1; i <= TotalPages; i++)
                {
                    if (i == PageIndex)
                    {
                        sb.AppendLine(string.Format("<li class=\"active\"><span>{0}</span></li>", i));
                    }
                    else
                    {
                        sb.AppendLine(string.Format("<li><a href=\"javascript:doSearch('{0}',{1});\" >{1}</a></li>", urlPrefix, i));
                    }
                }
            }


            //设置下一页
            if (PageIndex < TotalPages)
            {
                sb.AppendLine(string.Format("<li><a href=\"javascript:doSearch('{0}',{1});\">&raquo;</a></li>", urlPrefix, PageIndex + 1));
            }


            return new HtmlString(sb.ToString());
        }


        /// <summary>
        /// 分页方法（Ajax）
        /// </summary>
        /// <param name="helper">但前页对象</param>
        /// <param name="PageIndex">但前页</param>
        /// <param name="PageSize">每页显示的记录数</param>
        /// <param name="TotalCount">总记录数</param>
        /// <param name="TotalPages">总页数</param>
        /// <param name="urlPrefix">Url</param>
        /// <returns>分页控件</returns>
        public static IHtmlContent Pager(this IHtmlHelper helper, int PageIndex, int PageSize, int TotalCount, int TotalPages, string urlPrefix, string xxx)
        {
            //判断总页数为1的话就不显示分页
            if (TotalPages == 1)
                return new HtmlString("");
            StringBuilder sb = new StringBuilder();

            PageIndex = PageIndex < 1 ? 1 : PageIndex;                                                          //但前页如果小于1直接赋值为1
            PageIndex = PageIndex > TotalPages ? TotalPages : PageIndex;                       //但前页如果大于总页数直接赋值为最后一页
            int start = TotalPages - PageIndex >= 2 ? PageIndex - 2 : TotalPages - 4;        //设置要显示出来的第一个页码

            sb.AppendLine("<div class='divright'>");

            //设置第一页和上一页
            if (PageIndex > 1)
            {
                sb.AppendLine(string.Format("<a href='#' onclick=getDateAjax(\"{0}\",\"{1}\") >第一页</a>", urlPrefix, 1));
                sb.AppendLine(string.Format("<a href='#' onclick=getDateAjax(\"{0}\",\"{1}\") >上一页</a>", urlPrefix, PageIndex - 1));
            }
            else
            {
                sb.AppendLine("<span class='disabled'>第一页</span>");
                sb.AppendLine("<span class='disabled'>上一页</span>");
            }

            //设置要显示的页码
            int end = 1;
            while (end <= 5 && end <= TotalPages)
            {
                if (start == PageIndex)
                {
                    sb.AppendLine(string.Format("<span class='current'>{0}</span>", start));
                    start++;
                    end++;
                }
                else if (start > 0)
                {
                    sb.AppendLine(string.Format("<a  href='#' onclick=getDateAjax(\"{0}\",\"{1}\")>{1}</a>", urlPrefix, start));
                    start++;
                    end++;
                }
                else
                {
                    start++;
                }
            }

            //设置最后一页和下一页
            if (PageIndex < TotalPages)
            {
                sb.AppendLine(string.Format("<a  href='#' onclick=getDateAjax(\"{0}\",\"{1}\") >下一页</a>", urlPrefix, PageIndex + 1));
                sb.AppendLine(string.Format("<a  href='#' onclick=getDateAjax(\"{0}\",\"{1}\") >最后页</a>", urlPrefix, TotalPages));
            }
            else
            {
                sb.AppendLine("<span class='disabled'>下一页</span>");
                sb.AppendLine("<span class='disabled'>最后页</span>");
            }

            sb.AppendLine("</div>");

            sb.AppendLine("<div class='divleft'>");
            sb.AppendLine("<span style='padding-top: 2px;'>");
            sb.AppendLine(string.Format("<Input id= 'txtgo' type='text' class = 'txtgo' value={0}>", PageIndex));
            sb.AppendLine("</span>");
            sb.AppendLine(string.Format("<button class='btngo' onclick=getDateAjax(\"{0}\",document.getElementById('txtgo').value)>", urlPrefix));
            sb.AppendLine("<img src='/images/icons/pager_go.png' alt='go' /></button>");
            sb.AppendLine(string.Format("<span>总记录：{0}</span>", TotalCount));
            sb.AppendLine(string.Format("<span>总页数：{0}/{1}</span>", PageIndex, TotalPages));

            sb.AppendLine("</div>");

            return new HtmlString(sb.ToString());
        }


        /// <summary>
        /// 分页方法（Ajax）
        /// </summary>
        /// <param name="helper">但前页对象</param>
        /// <param name="PageIndex">但前页</param>
        /// <param name="PageSize">每页显示的记录数</param>
        /// <param name="TotalCount">总记录数</param>
        /// <param name="TotalPages">总页数</param>
        /// <param name="urlPrefix">Url</param>
        /// <param name="ShowModel">显示模式：（ture - 表示完整模式、false - 表示精简模式）</param>
        /// <returns>分页控件</returns>
        public static IHtmlContent CusAjaxPager(this IHtmlHelper helper, int PageIndex, int PageSize, int TotalCount, int TotalPages, string urlPrefix, bool ShowModel)
        {
            return CusAjaxPager(helper, PageIndex, PageSize, TotalCount, TotalPages, urlPrefix, null, null, null, ShowModel);
        }

        /// <summary>
        /// 分页方法（Ajax）
        /// </summary>
        /// <param name="helper">但前页对象</param>
        /// <param name="PageIndex">但前页</param>
        /// <param name="PageSize">每页显示的记录数</param>
        /// <param name="TotalCount">总记录数</param>
        /// <param name="TotalPages">总页数</param>
        /// <param name="urlPrefix">Url</param>
        /// <param name="divId">AJax结果集填充位置</param>
        /// <param name="callback">AJax结果集填充完后的回调函数名称 可为空 如果页面存在函数AjaxCallBack 默认作为回调函数</param>
        /// <param name="ShowModel">显示模式：（ture - 表示完整模式、false - 表示精简模式）</param>
        /// <returns>分页控件</returns>
        public static IHtmlContent CusAjaxPager(this IHtmlHelper helper, int PageIndex, int PageSize, int TotalCount, int TotalPages, string urlPrefix, string gotoId, string divId, string callback, bool ShowModel)
        {
            //AJax结果集填充位置
            divId = string.IsNullOrEmpty(divId) ? "divTable" : divId;
            //string divId = "divTable";
            //判断总页数为1的话就不显示分页
            if (TotalPages <= 1)
                return new HtmlString("");
            StringBuilder sb = new StringBuilder();

            gotoId = string.IsNullOrEmpty(gotoId) ? "txtgo" : gotoId;

            PageIndex = PageIndex < 1 ? 1 : PageIndex;                                                          //但前页如果小于1直接赋值为1
            PageIndex = PageIndex > TotalPages ? TotalPages : PageIndex;                       //但前页如果大于总页数直接赋值为最后一页
            int start = TotalPages - PageIndex >= 2 ? PageIndex - 2 : TotalPages - 4;        //设置要显示出来的第一个页码

            string stra = "<a href=\"#\" onclick=\"CusAjaxPager('{0}/?page={1}','','" + divId + "'" + (string.IsNullOrEmpty(callback) ? "" : "," + callback) + ")\">";

            //设置第一页和上一页
            if (PageIndex > 1)
            {
                sb.AppendLine(String.Format(stra + "第一页</a>", urlPrefix, 1));
                sb.AppendLine(String.Format(stra + "上一页</a>", urlPrefix, PageIndex - 1));
            }
            else
            {
                sb.AppendLine("<span class='disabled'>第一页</span>");
                sb.AppendLine("<span class='disabled'>上一页</span>");
            }
            if (ShowModel)
            {
                //设置要显示的页码
                int end = 1;
                while (end <= 5 && end <= TotalPages)
                {
                    if (start == PageIndex)
                    {
                        sb.AppendLine(String.Format("<span class='current'>{0}</span>", start));
                        start++;
                        end++;
                    }
                    else if (start > 0)
                    {
                        sb.AppendLine(String.Format(stra + "{1}</a>", urlPrefix, start));
                        start++;
                        end++;
                    }
                    else
                    {
                        start++;
                    }
                }
            }
            //设置最后一页和下一页
            if (PageIndex < TotalPages)
            {
                sb.AppendLine(String.Format(stra + "下一页</a>", urlPrefix, PageIndex + 1));
                sb.AppendLine(String.Format(stra + "最后页</a>", urlPrefix, TotalPages));
            }
            else
            {
                sb.AppendLine("<span class='disabled'>下一页</span>");
                sb.AppendLine("<span class='disabled'>最后页</span>");
            }

            if (ShowModel)
            {
                sb.AppendLine("<span style='padding-top: 2px;'>");
                sb.AppendLine(String.Format("<Input id= '" + gotoId + "' type='text' class = 'txtgo' value={0}>", PageIndex));
                sb.AppendLine("</span>");
                sb.AppendLine(String.Format("<input type='button' class='btngo' onclick=\"javascript:CusAjaxPager('{0}/?page='+document.getElementById('" + gotoId + "').value,'','" + divId + "'" + (string.IsNullOrEmpty(callback) ? "" : "," + callback) + ")\">", urlPrefix));

                sb.AppendLine(String.Format("<span>总记录：{0}</span>", TotalCount));
            }
            sb.AppendLine(String.Format("<span>总页数：{0}/{1}</span>", PageIndex, TotalPages));

            return new HtmlString(sb.ToString());
        }

        #endregion 自定义分页控件

        #region 下拉框
        /// <summary>
        /// 生成下拉列表
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="SelectListName">下拉列表的Name值</param>
        /// <param name="SelectItems">数据源</param>
        /// <param name="UserSelect">自定义选项，如“---请选择---“</param>
        /// <param name="SelectedValue">默认选中项</param>
        /// <param name="Attributes">附加属性值，比如onchange=""之类</param>
        /// <returns></returns>
        public static IHtmlContent CusDropDownList(this IHtmlHelper helper, string SelectListName, IEnumerable<SelectListItem> SelectItems, string UserSelect, object SelectedValue, string Attributes)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<select");
            if (SelectListName.Trim() != "")
            {
                sb.Append(" id=\"" + SelectListName + "\" name=\"" + SelectListName + "\"");
            }
            else
            {
                return new HtmlString("");
            }

            if (Attributes.Trim() != "")
            {
                sb.Append(" " + Attributes.Trim());
            }

            sb.Append(">");
            if (UserSelect.Length > 0)
                sb.Append("<option value=''>" + UserSelect + "</option>");
            if (SelectItems != null)
            {
                foreach (SelectListItem item in SelectItems)
                {
                    if (SelectedValue != null && item.Value == SelectedValue.ToString())
                    {
                        sb.Append("<option value=\"" + item.Value + "\" selected=\"selected\">" + item.Text + "</option>");
                    }
                    else
                    {
                        sb.Append("<option value=\"" + item.Value + "\">" + item.Text + "</option>");
                    }
                }
            }
            sb.Append("</select>");

            return new HtmlString(sb.ToString());
        }
        #endregion 下拉框

        #region 选择日期时间文本框
        /// <summary>
        /// 选择日期时间文本框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Id">文本框ID</param>
        /// <returns></returns>
        public static IHtmlContent CusTextBoxDate(this IHtmlHelper helper, string Id)
        {
            return CusTextBoxDate(helper, Id, null, "", "");
        }
        public static IHtmlContent CusTextBoxDate(this IHtmlHelper helper, string Id, DateTime? value)
        {
            return CusTextBoxDate(helper, Id, value, "", "");
        }
        public static IHtmlContent CusTextBoxDate(this IHtmlHelper helper, string Id, string Attributes)
        {
            return CusTextBoxDate(helper, Id, null, Attributes, "");
        }
        public static IHtmlContent CusTextBoxDate(this IHtmlHelper helper, string Id, DateTime? value, string Attributes)
        {
            return CusTextBoxDate(helper, Id, value, Attributes, "");
        }

        public static IHtmlContent CusTextBoxDate(this IHtmlHelper helper, string Id, DateTime? value, string Attributes, string DateProperty)
        {
            return CusTextBoxDate(helper, Id, value, null, Attributes, DateProperty);
        }
        /// <summary>
        /// 选择日期时间文本框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Id">文本框ID</param>
        /// <param name="value">文本框值</param>
        /// <param name="Attributes">附加属性值，比如onclick=""之类</param>
        /// <param name="showTime">是否要显示时间（如：true - 要显示、false - 不要显示）</param>
        /// <param name="DateProperty">时间控件特性（如：是否显示清除按钮：isShowWeek:true）</param>
        /// <returns></returns>
        public static IHtmlContent CusTextBoxDate(this IHtmlHelper helper, string Id, DateTime? value, string name, string Attributes, string DateProperty)
        {
            return CusTextBoxDateTime(helper, Id, value, name, Attributes, false, DateProperty);
        }

        /// <summary>
        /// 选择日期时间文本框(包括时间)
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Id">文本框ID</param>
        /// <param name="Attributes">附加属性值，比如onclick=""之类</param>
        /// <returns></returns>
        public static IHtmlContent CusTextBoxDateTime(this IHtmlHelper helper, string Id, string Attributes)
        {
            return CusTextBoxDateTime(helper, Id, null, Attributes, "");
        }
        public static IHtmlContent CusTextBoxDateTime(this IHtmlHelper helper, string Id, DateTime? value)
        {
            return CusTextBoxDateTime(helper, Id, value, "", "");
        }
        public static IHtmlContent CusTextBoxDateTime(this IHtmlHelper helper, string Id, DateTime? value, string Attributes)
        {
            return CusTextBoxDateTime(helper, Id, value, Attributes, "");
        }
        /// <summary>
        /// 选择日期时间文本框(包括时间)
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Id">文本框ID</param>
        /// <param name="value">文本框值</param>
        /// <param name="Attributes">附加属性值，比如onclick=""之类</param>
        /// <param name="showTime">是否要显示时间（如：true - 要显示、false - 不要显示）</param>
        /// <param name="DateProperty">时间控件特性（如：是否显示清除按钮：isShowWeek:true）</param>
        /// <returns></returns>
        public static IHtmlContent CusTextBoxDateTime(this IHtmlHelper helper, string Id, DateTime? value, string Attributes, string DateProperty)
        {
            return CusTextBoxDateTime(helper, Id, value, null, Attributes, true, DateProperty);
        }

        /// <summary>
        /// 选择日期时间文本框(包括时间)
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Id">文本框ID,日期图片ID为：Id_img</param>
        /// <param name="value">文本框值</param>
        /// <param name="Attributes">附加属性值，比如onclick=""之类</param>
        /// <param name="showTime">是否要显示时间（如：true - 要显示、false - 不要显示）</param>
        /// <param name="DateProperty">时间控件特性（如：是否显示清除按钮：isShowWeek:true）</param>
        /// <returns></returns>
        public static IHtmlContent CusTextBoxDateTime(this IHtmlHelper helper, string Id, DateTime? value, string name, string Attributes, bool showTime, string DateProperty)
        {
            StringBuilder sb = new StringBuilder();
            string format = showTime ? "yyyy-MM-dd HH:mm:ss" : "yyyy-MM-dd";
            name = string.IsNullOrEmpty(name) ? Id : name;
            string fun = "";
            if (DateProperty.Length > 0)
                fun = showTime ? "WdatePicker({el:'" + Id + "',dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true," + DateProperty + "});" : "WdatePicker({el:'" + Id + "',dateFmt:'yyyy-MM-dd',readOnly:true," + DateProperty + "});";
            else
                fun = showTime ? "WdatePicker({el:'" + Id + "',dateFmt:'yyyy-MM-dd HH:mm:ss',readOnly:true});" : "WdatePicker({el:'" + Id + "',dateFmt:'yyyy-MM-dd',readOnly:true});";
            sb.Append("<div>");
            sb.Append("<span class=\"ipk\">");
            sb.Append("<input type=\"text\" id=\"" + Id + "\"  name=\"" + name + "\" readonly=\"readonly\"");
            if (value.HasValue && !string.Equals(DateTime.Parse(value.ToString()).ToString("yyyy-MM-dd"), "0001-01-01"))
                sb.Append("value=\"" + value.Value.ToString(format) + "\" ");
            sb.Append(Attributes + " />");
            sb.Append("<img id =\"" + Id + "_img\" src=\"/Images/default/date.png\" alt=\"点击选择日期\" onclick=\"" + fun + "\"/>");
            sb.Append("</span>");
            sb.Append("</div>");
            return new HtmlString(sb.ToString());
        }
        #endregion

        #region 选择文本框
        /// <summary>
        /// 选择文本框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="HidId">隐藏文本框ID</param>
        /// <param name="Id">显示文本框ID</param>
        /// <param name="CookieName"></param>
        /// <param name="Attributes">选择按钮附加属性值，比如onchange=""之类</param>
        /// <returns></returns>        
        public static IHtmlContent CusTextBoxSelect(this IHtmlHelper helper, string HidId, string Id, string CookieName, string Attributes)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("<div>");
            //隐藏部分
            sb.Append("<div style=\" width:0px; display:none;\">");
            sb.Append("<input id=\"" + HidId + "\" name=\"" + HidId + "\" type=\"text\" class=\"SearchTemp\" style=\"width:0px;\"/>");
            sb.Append("</div>");

            //显示部分 
            sb.Append("<div style=\"width:151px;height:20px; border:solid 1px #7F9DB9; margin-top:0px; margin-left:0px;\">");
            sb.Append("<input type=\"text\" id=\"" + Id + "\" readonly=\"readonly\" style=\"border:0px; width:130px;height:16px; margin-top:1px;\"/>");
            sb.Append("<img style=\"height:16px; margin-top:-2px;cursor:pointer;\" src=\"/Images/default/tag_clear.png\" alt=\"点击清空内容\" onclick=\"ClearTxt('" + HidId + "','" + Id + "','" + CookieName + "');\" />");
            sb.Append("</div>");

            //选择按钮
            //sb.Append("<div style=\"margin-left:160px; margin-top:-22px;\"><button type=\"button\" onclick=\"" + Attributes + "\"><img src=\"/images/icons/table_go.png\" alt=\"\" />选择</button></div>");
            sb.Append("<div style=\"margin-left:160px; margin-top:-22px;\"><input type=\"button\" onclick=\"" + Attributes + "\" value=\"选择\"/></div>");
            sb.Append("</div>");
            return new HtmlString(sb.ToString());
        }
        #endregion 选择文本框

        #region 智能提示层（通常显示在文本框后面）
        /// <summary>
        /// 智能提示层（通常显示在文本框后面）
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="divId">提示层ID(默认值为：searchresult)</param>
        /// <param name="width">提示层的宽度</param>
        /// <param name="Attributes">附加属性值，比如onchange=""之类</param>
        /// <returns>div层，默认class：resultdiv</returns>
        public static IHtmlContent CusDivRemind(this IHtmlHelper helper, string divId, string width, string Attributes)
        {
            StringBuilder sb = new StringBuilder();
            string resultdivId = "searchresult";
            if (divId.Length > 0)
            {
                resultdivId = divId;
            }
            sb.Append("<div id=\"" + resultdivId + "\" class=\"resultdiv\" style=\"display:none; width:" + width + ";\"></div>");
            return new HtmlString(sb.ToString());
        }
        #endregion 智能提示层（通常显示在文本框后面）

        #region 可输入下拉框

        /// <summary>
        /// 文本下拉框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="controlName">控件名称</param>
        /// <param name="selectListItems">控件初始化所有的选择项</param>
        /// <param name="enterEvent">回车事件</param>
        /// <param name="isSearch">是否搜索</param>
        /// <param name="initHidValue">隐藏域的值</param>
        /// <param name="initShowValue">文本框的显示值</param>
        /// <param name="isSaveValue">在选择项中没找到的话，是否清空当前值，true，不清空，false：为清空</param>
        /// <param name="attributes">属性信息</param>
        /// <returns></returns>
        public static IHtmlContent CusDropList(this IHtmlHelper helper, string controlName, IEnumerable<SelectListItem> selectListItems, string enterEvent = "", bool isSearch = false, string initHidValue = "", string initShowValue = "", bool isSaveValue = false, string attributes = "")
        {
            string saveValue = isSaveValue ? "true" : "false";
            var sb = new StringBuilder();
            sb.AppendFormat("<div id=\"controlId\" class=\"linkMan\">");
            sb.Append("<div id=\"controlId_bg\" class=\"templittle\">");
            sb.Append("<input type=\"hidden\" id=\"controlId_value\" name=\"controlId\"");
            if (!string.IsNullOrEmpty(initHidValue))
            {
                sb.AppendFormat(" value=\"{0}\" ", initHidValue);
            }
            if (isSearch)
            {
                sb.Append(" class=\"SearchTemp\"");
            }
            sb.Append("/>");
            sb.Append("<span class=\"ipk\">");
            sb.Append("<input type=\"text\" id=\"controlId_name\" name=\"controlId_name\" class=\"little\"");
            if (!string.IsNullOrEmpty(initShowValue))
            {
                sb.AppendFormat(" value=\"{0}\" ", initShowValue);
            }
            else
            {
                sb.Append(" value=\"请选择\" ");
                initShowValue = "请选择";
            }
            sb.Append(" onblur=\"unFocus('controlId')\"");
            sb.Append(" onfocus=\"dropFocus('controlId')\"");
            if (!string.IsNullOrEmpty(attributes))
            {
                sb.AppendFormat(" {0} ", attributes);
            }
            else
            {
                sb.AppendFormat(" {0} ", "style=\"width:138px\"");
            }
            sb.AppendFormat("onkeyup=\"dropTextChange('controlId',event,{0});{1}\" />", saveValue, enterEvent);
            sb.Append("<img src=\"/Images/table_drop.gif\" alt=\"单击选择\" onclick=\"imgClick('controlId')\" />");
            //sb.Append("<img class=\"linkMan_img\" src=\"/images/default/arrow_d.png\" id=\"controlId_img\"");
            //sb.Append(" onclick=\"dropFocus('controlId')\" />");
            sb.Append("</span>");
            sb.Append("</div>");
            sb.Append("<ul id=\"controlId_ul\" class=\"linkMan_ul hide2\">");
            //int count = 0;
            //foreach (SelectListItem item in selectListItems)
            //{
            //    sb.AppendFormat("<li  current=\"{0}\" tag='{1}' id='controlId_{1}' >", count, item.Value);
            //    sb.AppendFormat("{0}</li>");
            //    count++;
            //}
            sb.Append("</ul>");
            sb.Append("<div id=\"controlId_option_temp\" class=\"hide2\">");
            sb.Append("<select id=\"controlId_option\">");
            if (selectListItems != null)
            {
                //sb.AppendFormat("<option value=\"{0}\" tag=\"{1}\">{1}</option>", initHidValue, initShowValue);
                sb.AppendFormat("<option value=\"0\" tag=\"{0}\">{0}</option>", "请选择");
                ////todo: 做选择项初始化
                foreach (var item in selectListItems)
                {
                    sb.AppendFormat("<option value=\"{0}\" tag=\"{1}\">{1}</option>", item.Value, item.Text);
                }
            }
            sb.Append("</select>");
            sb.Append("</div></div>");
            return new HtmlString(sb.ToString().Replace("controlId", controlName));
        }

        /// <summary>
        /// 自定义文本下拉框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="controlName">控件名称</param>
        /// <param name="selectListItems">下拉选择项</param>
        /// <param name="isSearch">是否为搜索</param>
        /// <param name="initHidValue">初始化隐藏域的值</param>
        /// <param name="initShowValue">初始化文本框的值</param>
        /// <param name="isSaveValue">是否保存找不到的值</param>
        /// <param name="attributes">自定义样式</param>
        /// <returns></returns>
        public static IHtmlContent CusDropList(this IHtmlHelper helper, string controlName, IEnumerable<SelectListItem> selectListItems, bool isSearch = false, string initHidValue = "", string initShowValue = "", bool isSaveValue = false, string attributes = "")
        {
            return CusDropList(helper, controlName, selectListItems, "", isSearch, initHidValue, initShowValue, isSaveValue, attributes);
        }

        /// <summary>
        /// 自定义文本下拉框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="controlName">控件名称</param>
        /// <param name="selectListItems">下拉选择项</param>
        /// <param name="initHidValue">初始化隐藏域的值</param>
        /// <param name="initShowValue">初始化文本显示值</param>
        /// <param name="isSaveValue">是否保存值</param>
        /// <param name="attributes">自定义css样式</param>
        /// <returns></returns>
        public static IHtmlContent CusDropList(this IHtmlHelper helper, string controlName, IEnumerable<SelectListItem> selectListItems, string initHidValue = "", string initShowValue = "", bool isSaveValue = false, string attributes = "")
        {
            return CusDropList(helper, controlName, selectListItems, false, initHidValue, initShowValue, isSaveValue, attributes);
        }

        #endregion

        #region 搜索文本框

        /// <summary>
        /// 搜索文本框
        /// </summary>
        /// <param name="helper"></param>
        /// <param name="Id">搜索文本框Id属性</param>
        /// <param name="Name">搜索文本框Name属性</param>
        /// <param name="Text">搜索文本框初始文本，如：输入员工名称(不指定则默认为：输入搜索)</param>
        /// <param name="Attributes">文本框的附加属性值，比如：style="width:20px"之类</param>
        /// <param name="ImgEventName">单击右侧图片方法名称，如：ExeSearch()</param>
        /// <returns></returns>        
        public static IHtmlContent CusTextBoxSeach(this IHtmlHelper helper, string Id, string Name, string Text, string Attributes, string ImgEventName)
        {
            if (Text.Length == 0)
                Text = "输入搜索...";
            else
                Text = Text + "...";
            StringBuilder sb = new StringBuilder();
            sb.Append("<input type=\"text\" id=\"" + Id + "\" name=\"" + Name + "\" class=\"form-control SearchTemp\" title=\"" + Text + "\" value=\"" + Text + "\" ");
            sb.Append("onblur=\"if(this.value=='')this.value='" + Text + "';\" onfocus=\"if(this.value=='" + Text + "')this.value=''\" value=\"" + Text + "\"");
            //如果有单击事件才需要绑定
            if (ImgEventName.Length > 0)
            {
                //绑定文本框按下回车事件，事件与单击右侧图片事件一致
                sb.Append(" onkeydown=\"if(window.event.keyCode ==13)" + ImgEventName + ";\"");
                ImgEventName = " onclick = \"" + ImgEventName + "\" ";
            }
            sb.Append(" " + Attributes + " />");
            return new HtmlString(sb.ToString());
        }

        #endregion
    }
}